﻿using Microsoft.EntityFrameworkCore;
using NLog.Web;
using Sheep_Breeding.Infeartructure;
using Sheep_Breeding.Read.Api.Common;

namespace Sheep_Breeding.Read.Api.Extensions
{
    /// <summary>
    /// 拓展类方法  静态类  静态方法  this关键字
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        public static WebApplicationBuilder Inject(this WebApplicationBuilder builder)
        {
            // Add services to the container.

            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            //中文注释
            builder.Services.AddSwaggerGen(x =>
            {
                var file = Path.Combine(AppContext.BaseDirectory + "Sheep_Breeding.Read.Api.xml");
                x.IncludeXmlComments(file, true);
            });



            //配置EFcore数据库
            builder.Services.AddDbContext<EFDbContext>(x =>
            {
                x.UseMySql(builder.Configuration["ConnectionStrings:Con"]
                    ,ServerVersion.AutoDetect(builder.Configuration["ConnectionStrings:Con"]), action =>
                    {
                        //配置EFCore的重试策略
                        action.EnableRetryOnFailure(
                            maxRetryCount: 10,//最大重试次数
                            maxRetryDelay: TimeSpan.FromSeconds(60),//每次重试间隔时间最大延迟
                            errorNumbersToAdd: null
                            );
                    }).EnableSensitiveDataLogging();//启动敏感数据日志记录，使与调试 但在生产环境中应谨慎使用避免泄露敏感信息

            //配置日志记录
            x.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddFilter(
                category: DbLoggerCategory.Database.Name,//类别
                level: LogLevel.Information//控制台日志
                ).AddConsole()));
            });


            //注入
            builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BasreRepository<>));



            //AutoMapper
            builder.Services.AddAutoMapper(typeof(MapperingProfil));
            //区分大小写
            builder.Services.AddControllersWithViews().AddJsonOptions(p => p.JsonSerializerOptions.PropertyNamingPolicy = null);
            //NLog注入
            builder.Host.UseNLog();

            return builder;
        }
    }
}
